package com.mhkj.base.region.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mhkj.base.region.entity.TSysNation;
import com.mhkj.base.region.repository.NationRepository;
import com.mhkj.base.region.service.NationService;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

/**
 * @Author: Administrator
 * @Desc:
 * @Date: 16:03 2018/6/19
 */
@Service
public class NationServiceImpl extends BaseServiceImpl<TSysNation> implements NationService {

    @Resource
    private NationRepository nationRepository;

    @Resource
    public void setNationRepository(NationRepository nationRepository) {
        super.setBaseRepository(nationRepository);
    }

    @Override
    public List<TSysNation> findByPid(Long pid) {
        if (pid == 0L) {
            return new ArrayList<>();
        }
        List<TSysNation> sysNationList = nationRepository.findByParentId(pid);
        for (TSysNation tSysNation : sysNationList) {
            if (tSysNation.getParentId() != 1L) {
                Optional<TSysNation> byId = nationRepository.findById(tSysNation.getParentId());
                TSysNation tSysNation1 = byId.get();
                tSysNation.setFullName(tSysNation1.getProvince() + tSysNation.getCity());
            } else {
                tSysNation.setFullName(tSysNation.getProvince());
            }

        }
        return sysNationList;
    }

    @Override
    public TSysNation findById(Long id) {
        Optional<TSysNation> optional = nationRepository.findById(id);
        TSysNation tSysNation = optional.get();
        if (tSysNation.getCode().endsWith("0000")) {
            tSysNation.setFullName(tSysNation.getProvince() + tSysNation.getCity());
        }
        return tSysNation;
    }

    @Override
    public List<TSysNation> findAllItemNation(Long pid) {
        return nationRepository.findAllItemNation(pid);

    }

    @Override
    public List<TSysNation> findAllItemProvince() {
        return nationRepository.findAllItemProcince();
    }


}
